
package avaliacao.tagHandler;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class HeadTagHandler extends BodyTagSupport {
   private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public HeadTagHandler() {
        super();
    }

    private void writeTagBodyContent(JspWriter out, BodyContent bodyContent) throws IOException {
        bodyContent.writeOut(out);
        bodyContent.clearBody();
    }

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("   <META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            out.println("   <META NAME=\"Author\" CONTENT=\"Priscilla Lusie and Dayana Lole\">");
            out.println("   <META NAME=\"keyWords\" CONTENT=\"avaliação, MEC\">");
            out.println("   <META NAME=\"country\" CONTENT=\"Brasil\">");
            out.println("   <META NAME=\"language\" CONTENT=\"Portuguese\">" );

            if (this.title == null){
                out.println("<TITLE>MEC - Avalia&ccedil;&atilde;o</TITLE>");
            }
            else{
                out.println("<TITLE> "+this.title+" </TITLE>");
            }

            out.println("   <LINK HREF=\"css/styleMEC.css\" REL=\"stylesheet\" TYPE=\"text/css\" >");
            out.println("   <script type=\"text/javascript\" src=\"js/head.js\"></script>");
        } catch (Exception e){
            Logger.getLogger(HeadTagHandler.class.getName()).log(
            Level.WARNING, "[HEAD] Excessão: " + e.getMessage());
        }
        /*Request the creation of new buffer, a BodyContent on which to evaluate
        the body of this tag.
        Returned from doStartTag when it implements BodyTag. This is an illegal
         return value when the class does not implement BodyTag.*/
        return EVAL_BODY_BUFFERED;
    }

    @Override
    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            out.println("</HEAD>");
        } catch (Exception e){}
        return EVAL_PAGE;
    }

    @Override
    public int doAfterBody() throws JspException {
        try {
            // This code is generated for tags whose bodyContent is "JSP"
            BodyContent bodyCont = getBodyContent();
            JspWriter out = bodyCont.getEnclosingWriter();

            writeTagBodyContent(out, bodyCont);
        } catch (Exception ex) {
            handleBodyContentException(ex);
        }

        return EVAL_PAGE;
    }

    private void handleBodyContentException(Exception ex) throws JspException {
        // Since the doAfterBody method is guarded, place exception handing code here.
        throw new JspException("Error in Head tag", ex);
    }
}
